PLUGIN.Name = "Helper"
PLUGIN.DefaultOn = false
PLUGIN.Description = 
[[Draws an helper on targeted props for building. Colors are Red, Green, Blue which respectively stand for Front, Left, Up.]]
PLUGIN.Trigger = false


function PLUGIN:LoadParameters()
	self:AddParameter("size",    { Type = "slider" , Text = "Size (default 5)", Defaults = "5", Min = "0", Max = "10", Decimals = "0" } )
	self:AddParameter("mindist", { Type = "slider" , Text = "Minimum distance (0 to disable)", Defaults = "0", Min = "0", Max = "2048", Decimals = "0" } )
	
end

function PLUGIN:Load()	
	self.CO_R = Color(237, 78, 64, 255)
	self.CO_G = Color(0, 255, 0, 255)
	self.CO_B = Color(0, 195, 255, 255)
	self.CO_SH = Color(0, 0, 0, 128)
	
	self.LastTime = 0
	self.Active   = true
	
	self.EntAtt = nil
end

function PLUGIN:Unload()
end

function PLUGIN.HOOK:Think()
	if RealTime() < (self.LastTime + 0.12) then return end
	self.LastTime = RealTime()
	
	self.MyTraceData = utilx.GetPlayerTrace( LocalPlayer(), LocalPlayer():GetCursorAimVector() )
	self.MyTraceData.filter = {LocalPlayer() , LocalPlayer():GetVehicle()}
	self.MyTraceRes = util.TraceLine( self.MyTraceData )
	if (self.MyTraceRes.Hit) and (self.MyTraceRes.HitNonWorld) and ValidEntity(self.MyTraceRes.Entity) and (self.MyTraceRes.Entity ~= LocalPlayer()) then
		self.Active = true
		self.EntAtt = self.MyTraceRes.Entity
		
	else
		self.Active = false
		
	end
end

function PLUGIN.HOOK:HUDPaint()
	if not ValidEntity(self.EntAtt) then return end
	
	local mindist = self:GetNumber("mindist")
	local pos = self.EntAtt:GetPos()
	
	if (mindist > 0) and ((EyePos() - pos):Length() > mindist) then return end
	
	local dist = (pos - EyePos()):Length() * 0.3 * (0.2 + 0.8 * self:GetNumber("size") / 5)
	
	local pts = pos:ToScreen()
	local front  = (pos + self.EntAtt:GetForward() * dist):ToScreen()
	local right  = (pos + self.EntAtt:GetRight() * dist):ToScreen()
	local up     = (pos + self.EntAtt:GetUp() * dist):ToScreen()
	
	local ppx     = (pos + Vector(1,0,0) * 0.5 * dist):ToScreen()
	local ppy     = (pos + Vector(0,1,0) * 0.5 * dist):ToScreen()
	
	local pws =      pos + Vector(1,1,0) * 0.01 * dist
	local pms = 	 pws:ToScreen()
	local pwx     = (pws + Vector(1,0,0) * 0.5 * dist):ToScreen()
	local pwy     = (pws + Vector(0,1,0) * 0.5 * dist):ToScreen()
	
	local ang = self.EntAtt:GetAngles()
	
	-- Draw my Icons and Text
	surface.SetFont("DefaultSmall")
	
	----UNEFFICIENT CODE
	surface.SetDrawColor( 0, 195, 255, 255 )
	surface.DrawLine( pts.x, pts.y, up.x, up.y )
	draw.SimpleText( "Up (".. string.format("%.2f",ang.y)..").y", "ScoreboardText", up.x+1, up.y+1, self.CO_SH, TEXT_ALIGN_CENTER )
	draw.SimpleText( "Up ("..string.format("%.2f",ang.y)..").y", "ScoreboardText", up.x, up.y, self.CO_B, TEXT_ALIGN_CENTER )

	surface.SetDrawColor( 0, 255, 0, 255 )
	surface.DrawLine( pts.x, pts.y, right.x, right.y )
	draw.SimpleText( "Right ("..string.format("%.2f",ang.p)..").p", "ScoreboardText", right.x+1, right.y+1, self.CO_SH, TEXT_ALIGN_CENTER )
	draw.SimpleText( "Right ("..string.format("%.2f",ang.p)..").p", "ScoreboardText", right.x, right.y, self.CO_G, TEXT_ALIGN_CENTER )
	
	surface.SetDrawColor( 237, 78, 64, 255 )
	surface.DrawLine( pts.x, pts.y, front.x, front.y )
	draw.SimpleText( "Front ("..string.format("%.2f",ang.r)..").r", "ScoreboardText", front.x+1, front.y+1, self.CO_SH, TEXT_ALIGN_CENTER )
	draw.SimpleText( "Front ("..string.format("%.2f",ang.r)..").r", "ScoreboardText", front.x, front.y, self.CO_R, TEXT_ALIGN_CENTER )
	
	

	surface.SetDrawColor( 0, 255, 0, 255 )
	surface.DrawLine( pts.x, pts.y, ppy.x, ppy.y )
	surface.DrawLine( pms.x, pms.y, pwy.x, pwy.y )
	draw.SimpleText( "World +Y", "ScoreboardText", ppy.x+1, ppy.y+1, self.CO_SH, TEXT_ALIGN_CENTER )
	draw.SimpleText( "World +Y", "ScoreboardText", ppy.x, ppy.y, self.CO_G, TEXT_ALIGN_CENTER )
	
	surface.SetDrawColor( 237, 78, 64, 255 )
	surface.DrawLine( pts.x, pts.y, ppx.x, ppx.y )
	surface.DrawLine( pms.x, pms.y, pwx.x, pwx.y )
	draw.SimpleText( "World +X", "ScoreboardText", ppx.x+1, ppx.y+1, self.CO_SH, TEXT_ALIGN_CENTER )
	draw.SimpleText( "World +X", "ScoreboardText", ppx.x, ppx.y, self.CO_R, TEXT_ALIGN_CENTER )

end

